import Vue from 'vue'
import { getWechatConfig } from '@/api/user';

// 是否苹果手机
function isIOS() {
  const isIphone = navigator.userAgent.includes('iPhone')
  const isIpad = navigator.userAgent.includes('iPad')
  return isIphone || isIpad
}

export default {
  // 是否微信环境
  isWechat: () => {
    let flag = false;
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/micromessenger/i) == 'micromessenger') {
      /* 这是微信浏览器 */
      if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
        flag = true;
      } else {
        flag = false;
      }
    } else {  
      flag = false;
    }
    return flag;
  },

  // 初始化配置
  initJssdk: () => {
    // #ifdef H5
    return new Promise((resolve, reject) => {
      let path = location.href;
     
      if (isIOS()) {
        path = uni.getStorageSync('firstUrl');
      }
      console.log(345345,path)
      getWechatConfig({
        path: path
      }).then(result => {
        if (result && result.data) {
          Vue.prototype.$wechat.config(result.data);
          resolve('success');
        } else {
          reject('fail');
        }
      }).catch(() => {
        reject('fail');
      })
    }).catch(e => {})
    // #endif
  }
}
